﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter_9__Recursion_
{
    public static class Ex_9_4
    {
        static bool UseDynamicProgramming = true;

        public static SortedSet<SortableSortedSet<T>> GetAllSubsets<T>(SortableSortedSet<T> Set) where T : IComparable
        {
            if (Set == null || Set.Count == 0)
            {
                return new SortedSet<SortableSortedSet<T>>();
            }

            SortableSortedSet<T> HashSetCopy = new SortableSortedSet<T>(Set);
            T test = HashSetCopy.Last();
            HashSetCopy.Remove(test);

            SortedSet<SortableSortedSet<T>> Result = GetAllSubsets(HashSetCopy);
            SortedSet<SortableSortedSet<T>> Result2 = new SortedSet<SortableSortedSet<T>>(Result);
            foreach (SortableSortedSet<T> tempSet in Result)
            {
                SortableSortedSet<T> tempHashSet = new SortableSortedSet<T>(tempSet);
                tempHashSet.Add(test);
                Result2.Add(tempHashSet);
            }


            SortableSortedSet<T> newHashSet = new SortableSortedSet<T>();
            newHashSet.Add(test);
            Result2.Add(newHashSet);

            return Result2;
        }

        public static string GetStringOfSubsets<T>(SortedSet<SortableSortedSet<T>> SetOfSets) where T : IComparable
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (SortableSortedSet<T> Set in SetOfSets)
            {
                bool first = true;
                foreach (T Item in Set)
                { 
                    if (!first)
                        stringBuilder.Append(", ");
                    first = false;
                    stringBuilder.Append(Item.ToString());
                }
                stringBuilder.Append(Environment.NewLine);
            }

            return stringBuilder.ToString();
        }
    }
}
